package com.haorui.lygyd.employeemanager.controller;

import com.haorui.base.core.controller.BaseController;
import com.haorui.base.core.utils.BeanUtil;
import com.haorui.base.core.vo.PageableVo;
import com.haorui.lygyd.employeemanager.dto.MemberDto;
import com.haorui.lygyd.employeemanager.dto.YuangongpingjiaDto;
import com.haorui.lygyd.employeemanager.entity.MemberEntity;
import com.haorui.lygyd.employeemanager.exceldto.MemberExcelDto;
import com.haorui.lygyd.employeemanager.service.MemberService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 创建人: 系统生成
 * 日期: Sat Sep 02 20:04:29 CST 2017
 */
@RestController
@RequestMapping(value = "/member")
public class MemberController extends BaseController<MemberEntity,MemberDto> {
    private MemberService memberService;
    public MemberController(@Autowired MemberService memberService) {
        super(memberService);
        this.memberService=memberService;
    }

    @ApiOperation(value = "会员解锁锁定", notes = "返回修改后的会员信息", position = 2)
    @RequestMapping(value = "/jieSuoSuoDing", method = RequestMethod.PATCH)
    public MemberEntity updateMemberJieSuoSuoDing(@RequestBody MemberDto dto) {
        return  memberService.updateMemberJieSuoSuoDing(dto);
    }

    @ApiOperation(value = "会员批量解锁", notes = "返回修改后的会员信息", position = 2)
    @RequestMapping(value = "/jiesuoMember", method = RequestMethod.POST)
    public void  jiesuoMember(@RequestBody String[] memberIds) {
        memberService.jiesuoMember(memberIds);
    }

    @ApiOperation(value = "会员批量锁定", notes = "返回修改后的会员信息", position = 2)
    @RequestMapping(value = "/suodingMember", method = RequestMethod.POST)
    public void  suodingMember(@RequestBody String[] memberIds) {
        memberService.suodingMember(memberIds);
    }

    @ApiOperation(value = "修改密码", notes = "返回修改后的会员信息", position = 2)
    @RequestMapping(value = "/updatePass", method = RequestMethod.PATCH)
    public MemberEntity updateMemberPass(@RequestBody MemberDto dto) {
        if(!dto.getMiMa().equals("")){
            if(memberService.checkByMemberMiMaAndId(dto.getMiMa(),dto.getId())){
                return memberService.updateMemberPass(dto);
            }else{
                ResponseEntity.status(200).body("旧密码填写错误");
                return null;
            }
        }
        return null;
    }

    @ApiOperation(value = "重置密码", notes = "返回修改后的会员信息", position = 2)
    @RequestMapping(value = "/chongZhiPass", method = RequestMethod.PATCH)
    public MemberEntity chongZhiMemberPass(@RequestBody MemberDto dto)
    {
        return  memberService.updateMemberPass(dto);
    }


    @ApiOperation(value = "会员信息导出", notes = "返回实体列表", position = 2)
    @RequestMapping(value = "/exportMember",method = RequestMethod.POST)
    public void exportMember(HttpServletRequest request, HttpServletResponse response,@RequestBody PageableVo pageableVo) throws Exception {
        List<MemberExcelDto> memberExcelDtoList=new ArrayList<MemberExcelDto>();
        ExportParams params = new ExportParams("会员信息导出", "会员信息导出");
        List<MemberEntity> memberEntityList=memberService.findAll(pageableVo.getPageModel().pageRequest(),pageableVo.getSearchConditions()).getContent();
        for (MemberEntity memberEntity : memberEntityList) {
            MemberExcelDto memberExcelDto=new MemberExcelDto();
            BeanUtil.beanCopy(memberEntity,memberExcelDto);
            memberExcelDtoList.add(memberExcelDto);
        }

        Workbook workbook = ExcelExportUtil.exportExcel(params, MemberExcelDto.class,memberExcelDtoList);
        workbook.write(response.getOutputStream());
    }

    @ApiOperation(value = "评价", notes = "评价", position = 2)
    @RequestMapping(value = "/pingjia", method = RequestMethod.POST)
    public void pingjia(@RequestBody YuangongpingjiaDto dto) {
      memberService.pingjia(dto);
    }
}
